<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Services\SmsService;
use Illuminate\Http\Request;

class SettingSmsController extends Controller
{
    private $smsService;

    public function __construct(SmsService $smsService)
    {
        $this->smsService = $smsService;
    }

    public function index(Request $request)
    {
        $page = [
            'page_name' => 'sms',
            'title' => '短信',
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $list = $this->smsService->list($request);

        return view('admin.sms.index', ['data' => $list, 'page' => $page]);
    }

    public function add()
    {
        $page = [
            'page_name' => 'sms',
            'title' => '新增短信',
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];

        return view('admin.sms.add', ['page' => $page]);
    }

    public function edit(Request $request, $id)
    {
        $data = $this->smsService->getSmsContent($id);
        $page = [
            'page_name' => 'sms',
            'title' => '修改短信',
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];

        return view('admin.sms.edit', ['page' => $page, 'data' => $data ?? []]);

    }

    public function save(Request $request, $id = 0)
    {
        $this->smsService->save($request, $id);
        return redirect('/admin/setting/sms')->with('trash', $id === 0 ? '新增成功！' : '更新成功！');
    }
}